#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 3e5 + 10, INF = 0x3f3f3f3f;

int n;
int a[N];
int st[N], tt = 0;

int get_mex(int p) {
    int res = 0;
    if (p == 1) {
        for (int i = 0;;i++)
            if (i != a[p] && i != a[p + 1]) { res = i; break; }
    } else if (p == n) {
        for (int i = 0;;i++)
            if (i != a[p] && i != a[p - 1]) { res = i; break; }
    } else {
        for (int i = 0;;i++)
            if (i != a[p] && i != a[p - 1] && i != a[p + 1]) { res = i; break; }
    }
    return res;
}

void solve()
{
    cin >> n;
    a[0] = -INF;tt = 0;
    for (int i = 1;i <= n;i++) {
        cin >> a[i];
        if (a[i] > a[st[tt]]) st[tt = 1] = i;
        else if (a[i] == a[st[tt]]) st[++tt] = i;
    }
    int ans = -INF, mx = a[st[tt]];
    for (int i = 1;i <= tt;i++) {
        ans = max(ans, mx - get_mex(st[i]));
    }
    cout << ans << '\n';
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    cin >> T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}